﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;

using NUnit.Framework;
using Vowei.Core.Models;

namespace vowei.specs
{
    [Binding]
    public class task_feature_impl : SpecTestBase
    {
        private Project Project { get; set; }
        private Task ExpectedTask { get; set; }

        [BeforeScenario]
        public void CreateANewProjectForTest()
        {
            base.BeforeTestRun();
            Project = CreateProjectFromContext();
            ExpectedTask = CreateTaskHelper(Project);
        }

        [Given(@"Click create task link on the project details page")]
        public void GivenClickCreateTaskLinkOnTheProjectDetailsPage()
        {
            Selenium.Open(string.Format("/Project/Details/{0}", Project.ID.ToString("D")));
            WaitPageToLoad();
            Selenium.Click("//div[@id='main']/p[1]/a[4]");
            WaitPageToLoad();
        }

        [Given(@"Fill all necessary task properties")]
        public void GivenFillAllNecessaryTaskProperties()
        {
            Selenium.Type("Task_Title", ExpectedTask.Title);
            Selenium.Type("Task_AssignedTo", ExpectedTask.AssignedTo);
            Selenium.Click("Task_StartDate");
            Selenium.Click("link=22");
            Selenium.Click("Task_DueDate");
            Selenium.Click("link=22");
            Selenium.Select("BelongsToMilestone.ID", string.Format("label={0}", Project.Title));
        }

        [When(@"I press create")]
        public void WhenIPressCreate()
        {
            Selenium.Click("submit_btn");
            WaitPageToLoad();
        }

        [Then(@"A task should be created")]
        public void ThenATaskShouldBeCreated()
        {
            var actual = FindTaskByTitle(ExpectedTask.Title);
            Assert.AreEqual(ExpectedTask.AssignedTo, actual.AssignedTo);
            Assert.AreEqual(ExpectedTask.BelongsToMilestoneId, actual.BelongsToMilestoneId);
        }
        
        [Given(@"I am on the create task page")]
        public void GivenIAmOnTheCreateTaskPage()
        {
            Selenium.Open("/");
            WaitPageToLoad();
            Selenium.Click("link=创建任务");
            WaitPageToLoad();
        }
    }
}
